package com.example.springboot.controller;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/loginController")
public class loginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> loginRequest) {
        String username = loginRequest.get("username");
        String password = loginRequest.get("password");


        User user = userService.findByUsername(username);

        if (user != null && user.getPassword().equals(password)) {
            // 登录成功，返回用户信息
            Map<String, Object> data = new HashMap<>();
            data.put("id", user.getId());
            data.put("username", user.getUsername());
            data.put("name", user.getName());
            data.put("role", user.getRole());
            System.out.println(Result.success(data) );
            return Result.success(data) ;
        } else {
            // 登录失败，返回错误信息
            return Result.error("401", "用户名或密码错误");
        }

    }

}

